package com.qingzhuge.limiter.configuration;

import com.qingzhuge.cache.support.LongRedisSerializer;
import com.qingzhuge.limiter.support.RateLimiterCall;
import com.qingzhuge.limiter.support.RateLimiterHandler;
import com.qingzhuge.limiter.support.impl.RateLimiterCallImpl;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author zeroxiao
 * @date 2019/9/4 16:11
 */
@ConditionalOnClass(RedisConnectionFactory.class)
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RateLimiterAutoConfiguration {

    @Bean
    public RedisTemplate<String, Long> stringLongRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new LongRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean
    public RateLimiterHandler rateLimiterHandler(){
        return new RateLimiterHandler();
    }

    @Bean
    public RateLimiterCall rateLimiterCallImpl(){
        return new RateLimiterCallImpl();
    }
}
